// 引入封装好的 axios
import Axios from './axios'
const baseUrl = import.meta.env.VITE_APP_BASE_URL

// 自定义数据结构
interface Result {
  code: number,
  error: string,
  [propName: string]: string | number | boolean | object | Array<any> | any
}

interface ResultData<T = any> extends Result {
  result?: T,
  total?: number
}

// 实例化
const instance = Axios()
 
// 创建需要的请求方法: get post put delete
// url: 请求的接口地址
// params: 请求参数
// headers: 请求头
export default {
  get<T> (url: string, params?: object, headers: object = {}): Promise<ResultData<T>> {
    return instance.get(baseUrl + url, { params, headers })
  },
  post<T> (url: string, params?: object, headers: object = {}): Promise<ResultData<T>> {
    return instance.post(baseUrl + url, params, { headers })
  },
  put<T> (url: string, params?: object, headers: object = {}): Promise<ResultData<T>> {
    return instance.put(baseUrl + url, params, { headers })
  },
  delete<T> (url: string, params?: object, headers: object = {}): Promise<ResultData<T>> {
    return instance.delete(baseUrl + url, { params, headers })
  }
}